<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="用户姓名" prop="memberName">
        <el-input
          v-model="queryParams.memberName"
          placeholder="请输入用户姓名"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="密码(未启用)" prop="password" v-if="false">
        <el-input
          v-model="queryParams.password"
          placeholder="请输入密码(未启用)"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="成员等级" prop="memberLevel" v-if="false">
        <el-select v-model="queryParams.memberLevel" placeholder="请选择成员等级" clearable>
          <el-option
            v-for="dict in cl_member_level"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="成员学历" prop="memberEdu" v-if="false">
        <el-select v-model="queryParams.memberEdu" placeholder="请选择成员学历" clearable>
          <el-option
            v-for="dict in cl_member_edu"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="成员民族" prop="memberNationality" v-if="false">
        <el-input
          v-model="queryParams.memberNationality"
          placeholder="请输入成员民族"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="籍贯" prop="nativePlace" v-if="false">
        <el-input
          v-model="queryParams.nativePlace"
          placeholder="请输入籍贯"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="血型" prop="bloodType" v-if="false">
        <el-select v-model="queryParams.bloodType" placeholder="请选择血型" clearable>
          <el-option
            v-for="dict in cl_blood_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="政治面貌" prop="political" v-if="false">
        <el-select v-model="queryParams.political" placeholder="请选择政治面貌" clearable>
          <el-option
            v-for="dict in cl_political_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label=" 1 男 0 女 2未知" prop="sex" v-if="false">
        <el-select v-model="queryParams.sex" placeholder="请选择 1 男 0 女 2未知" clearable>
          <el-option
            v-for="dict in cl_sex"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="生日" prop="birthday" v-if="false">
        <el-date-picker clearable
          v-model="queryParams.birthday"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择生日">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="所在城市" prop="city" v-if="false">
        <el-input
          v-model="queryParams.city"
          placeholder="请输入所在城市"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所在行业" prop="industry" v-if="false">
        <el-input
          v-model="queryParams.industry"
          placeholder="请输入所在行业"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所在单位" prop="company" v-if="false">
        <el-input
          v-model="queryParams.company"
          placeholder="请输入所在单位"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="自我介绍" prop="introduction" v-if="false">
        <el-input
          v-model="queryParams.introduction"
          placeholder="请输入自我介绍"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="邮箱" prop="email" v-if="false">
        <el-input
          v-model="queryParams.email"
          placeholder="请输入邮箱"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号码" prop="mobile">
        <el-input
          v-model="queryParams.mobile"
          placeholder="请输入手机号码"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="身份证号码" prop="idCard" v-if="false">
        <el-input
          v-model="queryParams.idCard"
          placeholder="请输入身份证号码"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否被锁定冻结" prop="isLock" v-if="false">
        <el-input
          v-model="queryParams.isLock"
          placeholder="请输入是否被锁定冻结"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="判断由哪个成员介绍进入" prop="firstLeader" v-if="false">
        <el-input
          v-model="queryParams.firstLeader"
          placeholder="请输入判断由哪个成员介绍进入"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用于app 授权类似于session_id" prop="token" v-if="false">
        <el-input
          v-model="queryParams.token"
          placeholder="请输入用于app 授权类似于session_id"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="注册时间" prop="regTime" v-if="false">
        <el-date-picker clearable
          v-model="queryParams.regTime"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择注册时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="最后登录时间" prop="lastLogin" v-if="false">
        <el-date-picker clearable
          v-model="queryParams.lastLogin"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择最后登录时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="最后登录ip" prop="lastIp" v-if="false">
        <el-input
          v-model="queryParams.lastIp"
          placeholder="请输入最后登录ip"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="地址" prop="address" v-if="false">
        <el-input
          v-model="queryParams.address"
          placeholder="请输入地址"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="居住地所在区域" prop="area" v-if="false">
        <el-input
          v-model="queryParams.area"
          placeholder="请输入居住地所在区域"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="0未审核1审核启用2停用3删除" prop="status" v-if="false">
        <el-select v-model="queryParams.status" placeholder="请选择0未审核1审核启用2停用3删除" clearable>
          <el-option
            v-for="dict in cl_examine_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleGroupAdd"
          v-hasPermi="['ndpf:group:add']"
        >新增分组</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleGroupAllocation"
          v-hasPermi="['ndpf:group:edit']"
        >成员分组分配</el-button>
      </el-col>
      <el-col :span="1.5" v-if="false">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleGroupDelete"
          v-hasPermi="['ndpf:group:remove']"
        >成员分组删除</el-button>
      </el-col>
      <el-col :span="1.5" v-if="false">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['ndpf:group:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-tabs v-model="groupActiveName" class="demo-tabs" @tab-click="handleGroupClick" editable @tab-remove="handleTabRemove">
      <el-tab-pane label="全部" name="total">
        <el-table v-loading="loading" :data="memberList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center"/>
          <el-table-column label="用户编号" align="center" prop="memberSn" />
          <el-table-column label="用户姓名" align="center" prop="memberName" />
          <el-table-column label="邮箱" align="center" prop="email" />
          <el-table-column label="手机号码" align="center" prop="mobile" />
          <el-table-column label="积分" align="center" prop="score" />
          <el-table-column label="服务时长" align="center" prop="totalService" />
          <el-table-column label="已审核通过" align="center" prop="status">
            <template #default="scope">
              <dict-tag :options="cl_examine_status" :value="scope.row.status"/>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template #default="scope">
              <el-button  type="primary"  @click="handleUserInfo(scope.row)" v-hasPermi="['ndpf:member:edit']">查看</el-button>
              <el-button  type="danger"   @click="handleDelete(scope.row)" v-hasPermi="['ndpf:member:remove']">删除成员</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-tab-pane>
      <el-tab-pane 
        v-for="tab in groupList" 
        :key="tab.groupId"
        :label="tab.groupName" 
        :name="tab.groupId"
      >
        <el-table v-loading="loading" :data="memberList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center" v-if="false"/>
          <el-table-column label="用户姓名" align="center" prop="memberName" />
          <el-table-column label="邮箱" align="center" prop="email" />
          <el-table-column label="手机号码" align="center" prop="mobile" />
          <el-table-column label="积分" align="center" prop="score" />
          <el-table-column label="服务时长" align="center" prop="totalService" />
          <el-table-column label="已审核通过" align="center" prop="status">
            <template #default="scope">
              <dict-tag :options="cl_examine_status" :value="scope.row.status"/>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template #default="scope">
              <el-button  type="primary"  @click="handleUserInfo(scope.row)" v-hasPermi="['ndpf:member:edit']">查看</el-button>
              <el-button  type="danger"   @click="handleGroupMenberDelete(scope.row)" v-hasPermi="['ndpf:member:remove']">移出分组</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-tab-pane>
    </el-tabs>
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改成员信息对话框 -->
    <el-dialog :title="title" v-model="open" width="1200px" append-to-body>
      <el-tabs v-model="activeName" class="demo-tabs" @tab-click="handleClick" tab-position="top" style="height: 700px; overflow: auto;">
        <el-tab-pane label="基本信息" name="first">
          <el-form ref="memberRef" :model="form" :rules="rules" label-width="120px">.
            <el-row>
              <el-col :span="8">
                <el-form-item label="头像" prop="headPic">
                  <image-preview :src="form.headPic" :width="100" :height="100"/>
                  <!-- <image-upload v-model="form.headPic"/> -->
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="证件照" prop="cardPhoto">
                  <image-preview :src="form.cardPhoto" :width="100" :height="100"/>
                  <!-- <image-upload v-model="form.cardPhoto"/> -->
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="生活照" prop="photo">
                  <image-preview :src="form.photo" :width="100" :height="100"/>
                  <!-- <image-upload v-model="form.photo"/> -->
                </el-form-item>
                </el-col>
              <el-col :span="6">
                <el-form-item label="用户姓名" prop="memberName">
                  <el-input v-model="form.memberName" placeholder="" readonly input-style="color: #409eff;"/>
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="成员等级" prop="memberLevel">
                  <el-select v-model="form.memberLevel" placeholder="" disabled>
                    <el-option
                      v-for="dict in cl_member_level"
                      :key="dict.value"
                      :label="dict.label"
                      :value="dict.value"               
                      style="background-color: #f50c0c"
                    ></el-option>
                  </el-select>          
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="成员学历" prop="memberEdu">
                  <el-select v-model="form.memberEdu" placeholder="" disabled >
                    <el-option
                      v-for="dict in cl_member_edu"
                      :key="dict.value"
                      :label="dict.label"
                      :value="dict.value"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="成员民族" prop="memberNationality">
                  <el-input v-model="form.memberNationality" placeholder="" readonly input-style="color: #409eff;"/>
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="籍贯" prop="nativePlace">
                  <el-input v-model="form.nativePlace" placeholder="" readonly input-style="color: #409eff;"/>
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="血型" prop="bloodType">
                  <el-select v-model="form.bloodType" placeholder="" disabled>
                    <el-option
                      v-for="dict in cl_blood_type"
                      :key="dict.value"
                      :label="dict.label"
                      :value="dict.value"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="政治面貌" prop="political">
                  <el-select v-model="form.political" placeholder="" disabled>
                    <el-option
                      v-for="dict in cl_political_type"
                      :key="dict.value"
                      :label="dict.label"
                      :value="dict.value"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="特长" prop="talent">
                  <el-checkbox-group v-model="form.talent">
                    <el-checkbox disabled 
                      v-for="dict in cl_talent_type"
                      :key="dict.value"
                      :label="dict.value">
                      {{dict.label}}
                    </el-checkbox>
                  </el-checkbox-group>
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="服务类别" prop="serviceType">
                  <el-checkbox-group v-model="form.serviceType">
                    <el-checkbox disabled 
                      v-for="dict in cl_team_service_type"
                      :key="dict.value"
                      :label="dict.value">
                      {{dict.label}}
                    </el-checkbox>
                  </el-checkbox-group>
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="服务时间" prop="serviceTime">
                  <el-checkbox-group v-model="form.serviceTime">
                    <el-checkbox disabled 
                      v-for="dict in cl_service_time"
                      :key="dict.value"
                      :label="dict.value">
                      {{dict.label}}
                    </el-checkbox>
                  </el-checkbox-group>
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="服务领域或行业" prop="serviceField">
                  <el-checkbox-group v-model="form.serviceField">
                    <el-checkbox disabled 
                      v-for="dict in cl_service_field"
                      :key="dict.value"
                      :label="dict.value">
                      {{dict.label}}
                    </el-checkbox>
                  </el-checkbox-group>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label=" 性别" prop="sex">
                  <el-select v-model="form.sex" placeholder="" disabled>
                    <el-option
                      v-for="dict in cl_sex"
                      :key="dict.value"
                      :label="dict.label"
                      :value="dict.value"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="生日" prop="birthday">
                  <el-date-picker clearable disabled
                    v-model="form.birthday"
                    type="date"
                    value-format="YYYY-MM-DD"
                    placeholder="">
                  </el-date-picker>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="所在城市" prop="city">
                  <el-input v-model="form.city" placeholder="" readonly input-style="color: #409eff;"/>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="所在行业" prop="industry">
                  <el-input v-model="form.industry" placeholder="" readonly input-style="color: #409eff;"/>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="所在单位" prop="company">
                  <el-input v-model="form.company" placeholder="" readonly input-style="color: #409eff;"/>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="邮箱" prop="email">
                  <el-input v-model="form.email" placeholder="" readonly input-style="color: #409eff;"/>
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="自我介绍" prop="introduction">
                  <el-input v-model="form.introduction" type="textarea" placeholder="" :autosize="{ minRows: 6, maxRows: 20 }" readonly input-style="color: #409eff;"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="手机号码" prop="mobile">
                  <el-input v-model="form.mobile" placeholder="" readonly input-style="color: #409eff;"/>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="身份证号码" prop="idCard">
                  <el-input v-model="form.idCard" placeholder="" readonly input-style="color: #409eff;"/>
                </el-form-item>      
              </el-col>  
              <el-col :span="8">
                <el-form-item label="注册时间" prop="regTime">
                  <el-date-picker clearable disabled
                    v-model="form.regTime"
                    type="date"
                    value-format="YYYY-MM-DD"
                    placeholder="">
                  </el-date-picker>
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="地址" prop="address">
                  <el-input v-model="form.address" placeholder="" readonly input-style="color: #409eff;"/>
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="居住地所在区域" prop="area">
                  <el-input v-model="form.area" placeholder="" readonly input-style="color: #409eff;"/>
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="已审核通过" prop="status">
                  <el-select v-model="form.status" placeholder="" disabled>
                    <el-option
                      v-for="dict in cl_examine_status"
                      :key="dict.value"
                      :label="dict.label"
                      :value="dict.value"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="6" v-if="false">
                <el-form-item label="备注" prop="remark" >
                  <el-input v-model="form.remark" type="textarea" placeholder="" />
                </el-form-item>
              </el-col>
              <el-col :span="6" v-if="false">
                <el-form-item label="最后登录时间" prop="lastLogin">
                  <el-date-picker clearable
                    v-model="form.lastLogin"
                    type="date"
                    value-format="YYYY-MM-DD"
                    placeholder="">
                  </el-date-picker>
                </el-form-item>
              </el-col>
              <el-col :span="6" v-if="false">
                <el-form-item label="最后登录ip" prop="lastIp">
                  <el-input v-model="form.lastIp" placeholder="" />
                </el-form-item>
              </el-col> 
              <el-col :span="6" v-if="false">
                <el-form-item label="是否被锁定冻结" prop="isLock">
                  <el-input v-model="form.isLock" placeholder="" />
                </el-form-item>
              </el-col>
              <el-col :span="6" v-if="false">
                <el-form-item label="判断由哪个成员介绍进入" prop="firstLeader">
                  <el-input v-model="form.firstLeader" placeholder="" />
                </el-form-item>
              </el-col>
              <el-col :span="6" v-if="false">
                <el-form-item label="用于app 授权类似于session_id" prop="token">
                  <el-input v-model="form.token" placeholder="" />
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
          <template #footer>
            <div class="dialog-footer">
              <el-button type="primary" @click="submitForm" v-if="false">确 定</el-button>
              <el-button type="warning" @click="cancel">关 闭</el-button>
            </div>
          </template>
        </el-tab-pane>
        <el-tab-pane label="参加的项目" name="second">
          <el-table :data="projectsList">
            <el-table-column type="selection" width="55" align="center" v-if="false"/>
            <el-table-column label="项目表id" align="center" prop="projectId" v-if="false"/>
            <el-table-column label="团体ID" align="center" prop="teamId" v-if="false"/>
            <el-table-column label="项目名称" align="center" prop="projectName" />
            <el-table-column label="所在区域" align="center" prop="area" :formatter="formatRegion" show-overflow-tooltip/>
            <el-table-column label="协议,用户须知" align="center" prop="projectProtocol" v-if="false"/>
            <el-table-column label="项目描述" align="center" prop="projectDesc" v-if="false"/>
            <el-table-column label="项目开始时间" align="center" prop="startProject" width="180" v-if="false">
              <template #default="scope">
                <span>{{ parseTime(scope.row.startProject) }}</span>
              </template>
            </el-table-column>
            <el-table-column label="项目结束时间" align="center" prop="endProject" width="180" v-if="false">
              <template #default="scope">
                <span>{{ parseTime(scope.row.endProject) }}</span>
              </template>
            </el-table-column>
            <el-table-column label="项目招募开始时间" align="center" prop="startPublic" width="180" v-if="false">
              <template #default="scope">
                <span>{{ parseTime(scope.row.startPublic) }}</span>
              </template>
            </el-table-column>
            <el-table-column label="项目招募结束时间" align="center" prop="endPublic" width="180" v-if="false">
              <template #default="scope">
                <span>{{ parseTime(scope.row.endPublic) }}</span>
              </template>
            </el-table-column>
            <el-table-column label="审计部门" align="center" prop="auditGroup" v-if="false"/>
            <el-table-column label="邮箱" align="center" prop="email" v-if="false"/>
            <el-table-column label="主要联系人" align="center" prop="projecContacts" />
            <el-table-column label="主要联系人电话" align="center" prop="projecPhone" />
            <el-table-column label="项目海报" align="center" prop="poster" width="100" v-if="false">
              <template #default="scope">
                <image-preview :src="scope.row.poster" :width="50" :height="50"/>
              </template>
            </el-table-column>
            <el-table-column label="服务类别" align="center" prop="projectType" v-if="false">
              <template #default="scope">
                <dict-tag :options="cl_team_service_type" :value="scope.row.projectType"/>
              </template>
            </el-table-column>
            <el-table-column label="服务对象" align="center" prop="serviceObject" v-if="false">
              <template #default="scope">
                <dict-tag :options="c_service_object" :value="scope.row.serviceObject"/>
              </template>
            </el-table-column>
            <el-table-column label="要求" align="center" prop="require" v-if="false"/>
            <el-table-column label="完成情况" align="center" prop="jobDone" v-if="false"/>
            <el-table-column label="所在城市" align="center" prop="city" v-if="false"/>
            <el-table-column label="项目照片" align="center" prop="projectPhoto" width="100" v-if="false">
              <template #default="scope">
                <image-preview :src="scope.row.projectPhoto" :width="50" :height="50"/>
              </template>
            </el-table-column>
            <el-table-column label="积分" align="center" prop="score" v-if="false"/>
            <el-table-column label="服务时长" align="center" prop="serviceTime" />
            <el-table-column label="已报名的人数" align="center" prop="number" v-if="false"/>
            <el-table-column label="招募人数要求" align="center" prop="numberDesc" v-if="false"/>
            <el-table-column label="招募人数" align="center" prop="numberLimit" v-if="false"/>
            <el-table-column label="地址" align="center" prop="address" v-if="false"/>
            <el-table-column label="经度" align="center" prop="longitude" v-if="false"/>
            <el-table-column label="纬度" align="center" prop="latitude" v-if="false"/>
            <el-table-column label="经纬度" align="center" prop="lonlat" v-if="false"/>
            <el-table-column label="项目二维码" align="center" prop="projectQrcode" width="100" v-if="false">
              <template #default="scope">
                <image-preview :src="scope.row.projectQrcode" :width="50" :height="50"/>
              </template>
            </el-table-column>
            <el-table-column label="项目状态" align="center" prop="status" v-if="false">
              <template #default="scope">
                <dict-tag :options="cl_pexamine_status" :value="scope.row.status"/>
              </template>
            </el-table-column>
            <el-table-column label="备注" align="center" prop="remark" v-if="false"/>
            <el-table-column label="加入时间" align="center" prop="createTime" width="180">
              <template #default="scope">
                <span>{{ parseTime(scope.row.createTime) }}</span>
              </template>
            </el-table-column>
          </el-table>
        </el-tab-pane>
        <el-tab-pane label="加入的队伍" name="third">
          <el-table :data="teamList">
            <el-table-column label="团队名称" align="center" prop="companyName" />
            <el-table-column label="所在区域" align="center" prop="area">
              <template #default="scope">
                <dict-tag :options="cl_team_area" :value="scope.row.area"/>
              </template>
            </el-table-column>
            <el-table-column label="负责人" align="center" prop="leader" />
            <el-table-column label="负责人联系方式" align="center" prop="leaderPhone" width="130"/>
          </el-table>
        </el-tab-pane>
        <el-tab-pane label="服务时长" name="fourth">Task</el-tab-pane>
      </el-tabs>
    </el-dialog>
    <!-- 添加或修改团队分组信息对话框 -->
    <el-dialog :title="groupTitle" v-model="openGroup" width="500px" append-to-body>
      <el-form ref="groupRef" :model="groupForm" :rules="groupRules" label-width="80px">
        <el-form-item label="分组名称" prop="groupName">
          <el-input v-model="groupForm.groupName" placeholder="请输入分组名称" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="groupForm.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitGroupForm">确 定</el-button>
          <el-button @click="cancelGroup">取 消</el-button>
        </div>
      </template>
    </el-dialog>
    <!-- 批量添加团队用户分组信息对话框 -->
    <el-dialog :title="成员分组分配" v-model="openGroupAllocation" width="500px" append-to-body>
      <el-form ref="groupRef" :model="groupForm" :rules="groupRules" label-width="80px">
        <el-form-item label="分组名称" prop="groupName">
          <el-select v-model="groupForm.groupName" placeholder="请选择分组" clearable>
            <el-option
              v-for="group in groupList"
              :key="group.groupId"
              :label="group.groupName"
              :value="group.groupId"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="handleGroupAllocationAdd">分组确定</el-button>
          <el-button @click="handleGroupAllocationCancle">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Member">
import { listMember, getMember, delMember, addMember, updateMember, listTeamMember, listTeamGroupMember, delTeamMember } from "@/api/ndpf/member";
import { listRegion, getRegion, delRegion, addRegion, updateRegion } from "@/api/ndpf/region";
import { listUserProjects } from "@/api/ndpf/projects";
import { listUserTeams } from "@/api/ndpf/team";
import { listGroup, getGroup, delGroup, addGroup, updateGroup } from "@/api/ndpf/group";
import { delTeamGroupMember, addTeamGroupMember, updateTeamGroupMember } from "@/api/ndpf/teamGroupMember";

const { proxy } = getCurrentInstance();
const { cl_sex, cl_member_level, cl_examine_status, cl_service_time, cl_talent_type, cl_member_edu, cl_service_field, cl_blood_type, cl_team_service_type, cl_political_type, cl_team_area } = proxy.useDict('cl_sex', 'cl_member_level', 'cl_examine_status', 'cl_service_time', 'cl_talent_type', 'cl_member_edu', 'cl_service_field', 'cl_blood_type', 'cl_team_service_type', 'cl_political_type', 'cl_team_area');

const memberList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const memberNames = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const projectsList = ref([]);
const teamList = ref([]);
const addressOptions = ref([]);
const activeName = ref('first');
const openGroup = ref(false);
const groupTitle = ref("");
const groupList = ref([]);
const groupActiveName = ref("total");
const openGroupAllocation = ref(false);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    memberName: null,
    password: null,
    memberLevel: null,
    memberEdu: null,
    memberNationality: null,
    nativePlace: null,
    bloodType: null,
    political: null,
    sex: null,
    birthday: null,
    city: null,
    industry: null,
    company: null,
    introduction: null,
    email: null,
    mobile: null,
    mobileValidated: null,
    idCard: null,
    idValidated: null,
    headPic: null,
    cardPhoto: null,
    photo: null,
    isLock: null,
    firstLeader: null,
    token: null,
    regTime: null,
    lastLogin: null,
    lastIp: null,
    idAudit: null,
    score: null,
    serviceTime: null,
    address: null,
    area: null,
    status: null,
    teamId: null,
    userId: null,
    groupId: null,
    groupName: null,
  },
  rules: {
    memberEdu: [
      { required: true, message: "成员学历不能为空", trigger: "change" }
    ],
    status: [
      { required: true, message: "0未审核1审核启用2停用3删除不能为空", trigger: "change" }
    ],
  },
  groupForm: {},
  groupRules: {
    groupName: [
      { required: true, message: "分组名称不能为空", trigger: "change" }
    ]
  },
});

const { queryParams, form, rules, groupForm, groupRules } = toRefs(data);

/** 查询成员信息列表 */
function getList() {
  loading.value = true;
  listTeamMember(queryParams.value).then(response => {
    memberList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

/** 查询成员分组信息列表 */
function getGroupMemberList() {
  loading.value = true;
  listTeamGroupMember(queryParams.value).then(response => {
    memberList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function groupReset() {
  groupForm.value = {
    groupName: null,
    remark: null
  };
  proxy.resetForm("memberRef");
}

// 表单重置
function reset() {
  form.value = {
    memberId: null,
    userId: null,
    memberName: null,
    password: null,
    memberLevel: null,
    memberEdu: null,
    memberNationality: null,
    nativePlace: null,
    bloodType: null,
    political: null,
    talent: [],
    serviceType: [],
    serviceTime: [],
    serviceField: [],
    sex: null,
    birthday: null,
    city: null,
    industry: null,
    company: null,
    introduction: null,
    email: null,
    mobile: null,
    mobileValidated: null,
    idCard: null,
    idValidated: null,
    headPic: null,
    cardPhoto: null,
    photo: null,
    isLock: null,
    firstLeader: null,
    token: null,
    regTime: null,
    lastLogin: null,
    lastIp: null,
    idAudit: null,
    score: null,
    totalService: null,
    address: null,
    area: null,
    status: null,
    remark: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null
  };
  proxy.resetForm("memberRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.memberId);
  memberNames.value = selection.map(item => item.memberName);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加成员信息";
}

/** 修改按钮操作 */
function handleUserInfo(row) {
  reset();
  const _memberId = row.memberId || ids.value
  getMember(_memberId).then(response => {
    form.value = response.data;
    form.value.talent = (form.value.talent || '').split(',');
    form.value.serviceType = (form.value.serviceType || '').split(',');
    form.value.serviceTime = (form.value.serviceTime || '').split(',');
    form.value.serviceField = (form.value.serviceField || '').split(',');
    open.value = true;
    title.value = "查看成员信息";
  });
  queryParams.value.teamId = row.teamId;
  queryParams.value.userId = row.userId;
  console.log(queryParams.value);
  listUserProjects(queryParams.value).then(response => {
    projectsList.value = response.data;
  });

    listUserTeams(queryParams.value).then(response => {
    teamList.value = response.data;
  });
  activeName.value = ref('first')
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["memberRef"].validate(valid => {
    if (valid) {
      form.value.talent = form.value.talent.join(",");
      form.value.serviceType = form.value.serviceType.join(",");
      form.value.serviceTime = form.value.serviceTime.join(",");
      form.value.serviceField = form.value.serviceField.join(",");
      if (form.value.memberId != null) {
        updateMember(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addMember(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const memberName = row.memberName;
  const memberId = row.memberId;
  const teamId = row.teamId;
  const userId = row.userId;
  // 将变量转换为JSON对象
  const jsonData = {
    memberId: memberId,
    teamId: teamId,
    userId: userId
  };
  proxy.$modal.confirm('是否确认删除成员为"' + memberName + '"的数据项？').then(function() {
    return delTeamMember(jsonData);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('ndpf/member/export', {
    ...queryParams.value
  }, `member_${new Date().getTime()}.xlsx`)
}

/** 查询区域信息列表 */
function getRegionList() {
  listRegion(queryParams.value).then(response => {
    addressOptions.value = buildTree(response.data);
  });
}

function buildTree(regions, parentId = 0) {
  const tree = [];
  regions.forEach(region => {
    if (Number(region.parentId) === Number(parentId)) {
      const children = buildTree(regions, region.regionId);
      if (children.length > 0) {
        region.children = children;
      }
      tree.push(region);
    }
  });
  return tree;
}

// 格式化显示地区
function formatRegion(row, column, cellValue) {
  if (!cellValue || cellValue.length === 0) return ''
  const labels = []
  let currentOptions = addressOptions.value;
  
  for (const value of cellValue) {
    const found = currentOptions.find(item => item.regionId === value)
    if (found) {
      labels.push(found.regionName)
      currentOptions = found.children || []
    } else {
      
    }
  }
  
  return labels.join(' / ')
}

//重置分组表单
function resetGroup() {
  form.value = {
    groupId: null,
    teamId: null,
    groupName: null,
    idDelete: null,
    status: null,
    remark: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null
  };
  proxy.resetForm("groupRef");
}

//新增分组
function handleGroupAdd(){
  groupReset();
  openGroup.value = true;
  groupTitle.value = "添加分组";
}

// 取消按钮
function cancelGroup() {
  openGroup.value = false;
  resetGroup();
}

/** 查询团队分组信息列表 */
function getGroupList() {
  listGroup().then(response => {
    groupList.value = response.data;
  });
}

/** 提交按钮 */
function submitGroupForm() {
  proxy.$refs["groupRef"].validate(valid => {
    if (valid) {
      if (groupForm.value.groupId != null) {
        updateGroup(groupForm.value).then(response => {
          proxy.$modal.msgSuccess("修改分组成功");
          openGroup.value = false;
          getGroupList();
        });
      } else {
        addGroup(groupForm.value).then(response => {
          proxy.$modal.msgSuccess("新增分组成功");
          openGroup.value = false;
          getGroupList();
        });
      }
    }
  });
}

function handleGroupClick(tab,ev){
  ids.value = "";
  if(tab.props.name == "total"){
    queryParams.value.groupId = "";
    getList();
    return;
  }
  let groupId = tab.props.name;
  let label = tab.props.label;
  queryParams.value.groupId = "";
  queryParams.value.groupId = groupId;
  getGroupMemberList();
}

function handleTabRemove(targetName){
  if(targetName == "total"){
    return;
  }
  let groupId = targetName;
  proxy.$modal.confirm('是否确认删除该分组信息').then(function() {
    return delGroup(groupId);
  }).then(() => {
    groupActiveName.value = "total";
    proxy.$modal.msgSuccess("删除成功");
    getGroupList();
  }).catch(() => {});
}

/* 成员分组分配*/
function handleGroupAllocation(){
  groupForm.value = {};
  openGroupAllocation.value = true;
}

function handleGroupAllocationAdd(){
  const _memberId = ids.value;
  if(_memberId ==="" || groupForm.value.groupName ===""){
    alert("成员和分组不能为空!");
    return;
  }
  groupForm.value.gmemberIds = _memberId;
  groupForm.value.groupId = groupForm.value.groupName;
  proxy.$modal.confirm('是否确认是否进行成员分组').then(function() {
    return addTeamGroupMember(groupForm.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          openGroupAllocation.value = false;
        }).catch(() => {});
    });
}

function handleGroupAllocationCancle(){
  openGroupAllocation.value = false;
}


/** 删除分组成员操作 */
function handleGroupMenberDelete(row) {
  const _gmemberIds = row.gmemberId
  proxy.$modal.confirm('是否确认删除姓名为“'+row.memberName+'”的分组组员？').then(function() {
    return delTeamGroupMember(_gmemberIds);
  }).then(() => {
    getGroupMemberList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

getList();
getRegionList();
getGroupList();
</script>

<style scoped>
:deep(.el-input__inner) {
  background-color: #FFFFFF;
}

:deep(.el-input.is-disabled .el-input__inner) {
  -webkit-text-fill-color: #409eff;
}

/* 修改禁用状态下的复选框字体颜色 */
/* :deep(.el-checkbox.is-disabled .el-checkbox__label) {
  color: #409eff;
}  */

:deep(.el-checkbox.is-disabled.is-checked .el-checkbox__inner) {
  background-color: #66ff94 ; /* 自定义打勾背景色（例如红色） */
  border-color: #66ff94 ;     /* 自定义边框颜色 */
}

</style>
